<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
    <title>boost 搜索引擎</title>
    <style>
        *{
            /* 外边距 */
            margin: 0;
            /* 内边距 */
            padding: 0;
        }
        html, body{
            height: 100%;
        }
        /* 类选择器.container */
        .container{
            /* 设置div的宽度 */
            width: 800px;
            margin: 0px auto;
            /* 设置div的上边距 */
            margin-top: 15px;
        }
        .container .search{
            width: 100%;
            height: 52px;
        }
        .container .search input{
            float: left;
            width: 600px;
            height: 50px;
            border: 1px solid black;
            border-right: none;
            padding-left: 10px;
            color: #CCC;
            font-size: 15px;
        }
        .container .search button{
            float: left;
            width: 120px;
            height: 52px;
            font-size: 19px;
            background-color: #4e6ef2;
            color:#FFF;
            font-family: Georgia, 'Times New Roman', Times, serif;
        }
        .container .result .item{
            margin-top: 15px;

        }
        .container .result .item a{
            /* 取消a标签的下划线 */
            display: block;
            text-decoration: none;
            font-size: 20px;
            color: #4e6ef2;
        }
        .container .result .item a:hover{
            text-decoration: underline;
        }
        .container .result .item p{
            margin-top: 5px;
            font-size: 16px;
            font-family: 'Lucida Sans', 'Lucida Sans Regular', 'Lucida Grande', 'Lucida Sans Unicode', Geneva, Verdana, sans-serif;
        }
        .container .result.item i{
            display: block;
            /* 取消斜体 */
            font-style: normal;
            color: green;
        }
    </style>    
</head>
<body>
    <div class="container">
        <div class="search">
            <input type="text" value="输入搜索关键字...">
            <button onclick="Search()">搜索一下</button>
        </div>
        <div class="result">
            <!-- <div class="item">
                <a href="#">这是标题</a>
                <p>这是内容</p>
                <i>http://search.gitee.com/?type=repository&q=cpp-httplib</i>
            </div>
            <div class="item">
                <a href="#">这是标题</a>
                <p>这是内容</p>
                <i>http://search.gitee.com/?type=repository&q=cpp-httplib</i>
            </div>
            <div class="item">
                <a href="#">这是标题</a>
                <p>这是内容</p>
                <i>http://search.gitee.com/?type=repository&q=cpp-httplib</i>
            </div> -->
        </div>
    </div>
    <script>
        function Search()
        {
            //alert("hello!");
            //$可以理解为JQuery的别称
            let query = $(".container .search input").val();
            console.log("query: =" + query);//console浏览器的对话框，可以用来进行查看js数据
            //发起http请求
            $.ajax({
                type: "GET",
                url: "/s?word=" + query,
                success: function (data) 
                {
                    console.log(data);
                    BuildHtml(data);
                }
            })
        }
        function BuildHtml(data)
        {
            // 获取html的result标签
            let result_lable = $(".container .result");
            // 清空result标签中的历史内容
            result_lable.empty();
            // 遍历data数据
            for (let elem of data)
            {
                let a_lable = $("<a>",{
                    text: elem.title,
                    href: elem.url,
                    // 跳转到新的页面
                    target: "_blank"
                });
                let p_lable = $("<p>",{
                    text: elem.desc
                });
                let i_lable = $("<i>",{
                    text: elem.url
                });
                let div_lable = $("<div>",{
                    class: "item"
                });
                a_lable.appendTo(div_lable);
                p_lable.appendTo(div_lable);
                i_lable.appendTo(div_lable);
                div_lable.appendTo(result_lable);
            }
        }
    </script>
</body>
</html>